<?php


namespace app\controller\admin;


use app\BaseController;
use app\common\Consts;
use app\common\OrderStatus;
use app\common\Service;
use app\models\user\ProgramProcessModel;
use app\models\user\ProgramProductModel;
use app\models\user\ServiceModel;
use think\facade\Db;

class ServiceController extends BaseAdminController
{
    function setSearch()
    {
        return [
            '=' => ['a.program_sn'],
        ];
    }

    function setSort()
    {
        return [
            'program_sn' => 'b.program_sn'
        ];
    }

    public function getList()
    {
        try {
            $where = $this->searchWhere;
            $orderBy = $this->orderBy;
            $sort = $this->sort;

            $list = ServiceModel::alias('a')
                ->field('a.*,b.program_sn,b.program_name,b.process,b.user_name')
                ->leftJoin('program_info b', 'a.program_sn = b.program_sn')
                ->where($where)
                ->order($orderBy, $sort)
                ->paginate(['list_rows' => $this->limit, 'page' => $this->page])
                ->toArray();
            foreach ($list['data'] as &$v) {
                $v['process_attr'] = ProgramProcessModel::where('level', $v['process'])->value('name');
            }
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function edit()
    {
        Db::startTrans();
        try {
            $data = $this->request->param();
            $id = $data['id'];
            if (!$id) {
                throw new \Exception('非法请求');
            }
            $info = Db::name('service')->where('id', $id)->find();
            if (!$info) {
                throw new \Exception('信息不存在');
            }
            $process = $data['process'];

            $data['start_time'] = strtotime($data['start_time']);
            $data['end_time'] = strtotime($data['end_time']);
            $data['update_time'] = time();

            unset($data['program_sn'], $data['user_name'], $data['program_name'], $data['id'], $data['process'], $data['process_name'],
                $data['status'], $data['create_time']);


            Db::name('service')->where('id', $id)->update($data);

            $userInfo = Db::name('user')->where('id', $data['user_id'])->find();
            if ($userInfo['process'] != $process) {
                Db::name('user')->where('id', $data['user_id'])->update([
                    'process' => $process,
                ]);
                Db::name('user_record')->insert([
                    'user_id' => $data['user_id'],
                    'write_user_id' => $this->request->userId,
                    'context' => '修改 项目进度：' . OrderStatus::getProcess($userInfo['process']) . '，改为：' . OrderStatus::getProcess($process),
                    'create_time' => getFormatDate()
                ]);
            }

            Db::commit();
            return $this->success();
        } catch (\Exception $exception) {
            Db::rollback();
            return $this->error($exception->getMessage());
        }
    }

    public function del()
    {
        Db::startTrans();
        try {
            $id = $this->request->param('id');
            if ($id <= 0) {
                throw new \Exception('非法请求');
            }
            $info = Db::name('service')->where('id', $id)->find();
            if (!$info) {
                throw new \Exception('信息不存在');
            }

            Db::name('service')->where('id', $id)->delete();
            Db::commit();
            return $this->success();
        } catch (\Exception $exception) {
            Db::rollback();
            return $this->error($exception->getMessage());
        }
    }


}